package com.ming.sps.server.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import com.ming.sps.server.bean.ParamMap;
import com.ming.sps.server.dao.Dao;
import com.ming.sps.server.utils.JsonUtil;

public class BaseAction {

    protected Dao dao = new Dao();


    protected HttpServletRequest getRequest() {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return req;
    }

    /**
     * 获得realpath
     *
     * @return
     */
    protected String getRealPath() {
        return getRequest().getSession().getServletContext().getRealPath("");
    }


    protected void setRequest(String key, Object value) {
        getRequest().setAttribute(key, value);
    }

    public HttpServletResponse getResponse() {
        HttpServletResponse resp = ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
        return resp;
    }

    /**
     * 获得会话
     *
     * @param key 对象key
     * @return 对象
     */
    protected <T> T getSessionValue(String key) {
        return (T) getRequest().getSession().getAttribute(key);
    }

    /**
     * 删除会话
     *
     * @param key 对象key
     * @return 对象
     */
    protected void removeSessionValue(String key) {
        getRequest().getSession().removeAttribute(key);
    }

    /**
     * 设置会话
     *
     * @param key   对象key
     * @param value 值
     */
    protected <T> void setSessionValue(String key, T value) {
        getRequest().getSession().setAttribute(key, value);
    }

    /**
     * 清空会话
     */
    protected void clearSession() {
        getRequest().getSession().invalidate();
    }


    protected Map<String, String[]> getParameterMap() {
        return getParameterMap(getRequest());
    }

    protected Map<String, String[]> getParameterMap(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        return requestMap;
    }

    protected Map requestParam(HttpServletRequest request) {
        Map<String, String[]> requestMap = getParameterMap(request);
        Map<String, Object> map = new HashMap();
        for (String key : requestMap.keySet()) {
            String value = requestMap.get(key)[0];
            if (!"token".equals(key)) {
                map.put(key, value);
            }
        }
        return map;
    }

    protected ParamMap requestParamByParamMap(HttpServletRequest request) {
        Map<String, String[]> requestMap = getParameterMap(request);
        ParamMap map = new ParamMap();
        for (String key : requestMap.keySet()) {
            String value = requestMap.get(key)[0];
            if (!"token".equals(key)) {
                map.put(key, value);
            }
        }
        return map;
    }
    protected String getRequest(String paramKey, String defaultValue) {
        Map<String, String[]> requestMap = getParameterMap();
        String paramValue = defaultValue;
        if (!requestMap.isEmpty()) {
            try {
                paramValue = requestMap.get(paramKey)[0];
            } catch (Exception e) {
                System.out.println(paramKey + " not found!");
            }
        }
        return paramValue;
    }
    protected String getRequest(HttpServletRequest request,String paramKey, String defaultValue) {
        Map<String, String[]> requestMap = request.getParameterMap();
        String paramValue = defaultValue;
        if (!requestMap.isEmpty()) {
            try {
                paramValue = requestMap.get(paramKey)[0];
            } catch (Exception e) {
                System.out.println(paramKey + " not found!");
            }
        }
        return paramValue;
    }

    /**
     * @param errorMsg 错误提示内容
     * @param sound    提示声音
     * @return
     * @throws Exception
     */
    protected String retError(String errorMsg, String sound) throws Exception {
        if (StringUtils.isBlank(errorMsg)) {
            errorMsg = " ";
        } else if (StringUtils.isBlank(sound)) {
            sound = "null";
        }
        return "error:" + errorMsg + "&sound:" + sound;
    }

    /**
     * @param successMsg 提示内容
     * @param sound      提示声音
     * @return
     * @throws Exception
     */
    protected String retSuccess(String successMsg, String sound) throws Exception {
        ParamMap retMap = new ParamMap();
        retMap.put("msg", successMsg);
        retMap.put("sound", sound);
        return JsonUtil.putToStr("success", retMap);
    }

}
